/* 
 * Copyright (C) 2012 Mark Thompson (mark.artifice@gmail.com)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.artificetech.videolibrary.metadata;

import com.artificetech.videolibrary.metadata.MP4VideoDataProvider.MP4Metadata;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MP4MetadataCache extends SQLiteOpenHelper{
	private static final String DATABASE_NAME="mp4metadatacache";
	private static final String TABLE_NAME="videos";
	private static final String COLUMN_PATH="path";
	private static final String COLUMN_LAST_MODIFIED="modified";
	private static final String COLUMN_TITLE="title";
	private static final String COLUMN_TITLE_SORT="titlesort";
	private static final String COLUMN_RELEASE_YEAR="year";
	private static final String COLUMN_DESCRIPTION="description";
	private static final String COLUMN_GENRE="genre";
	private static final String COLUMN_CAST="cast";
	private static final String COLUMN_DIRECTOR="director";
	private static final String COLUMN_COVER_ART="coverart";
	
	private static final int DATABASE_VERSION=2;
	
	MP4MetadataCache(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE "+TABLE_NAME+"("+
			COLUMN_PATH+" TEXT UNIQUE ON CONFLICT REPLACE, "+
			COLUMN_LAST_MODIFIED+" INTEGER, "+
			COLUMN_TITLE+" TEXT, "+
			COLUMN_TITLE_SORT+" TEXT, "+
			COLUMN_RELEASE_YEAR+" INTEGER, "+
			COLUMN_DESCRIPTION+" TEXT, "+
			COLUMN_GENRE+" TEXT, "+
			COLUMN_CAST+" TEXT, "+
			COLUMN_DIRECTOR+" TEXT, "+
			COLUMN_COVER_ART+" BLOB)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Just recreate it.
		db.execSQL("DROP TABLE "+TABLE_NAME);
		onCreate(db);
	}
	
	@Override
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Just recreate it.
		db.execSQL("DROP TABLE "+TABLE_NAME);
		onCreate(db);
	}
	
	public void addRecord(String path, long lastModified, MP4Metadata metadata) {
		SQLiteDatabase db=getWritableDatabase();
		
		ContentValues values=new ContentValues();
		values.put(COLUMN_PATH, path);
		values.put(COLUMN_LAST_MODIFIED, lastModified);
		values.put(COLUMN_TITLE, metadata.title);
		values.put(COLUMN_TITLE_SORT, metadata.titleSort);
		values.put(COLUMN_RELEASE_YEAR, metadata.year);
		values.put(COLUMN_DESCRIPTION, metadata.description);
		values.put(COLUMN_GENRE, metadata.genre);
		values.put(COLUMN_CAST, metadata.cast);
		values.put(COLUMN_DIRECTOR, metadata.director);
		values.put(COLUMN_COVER_ART, metadata.coverArt);
		db.insert(TABLE_NAME, null, values);
	}
	
	public MP4Metadata getRecord(String path, long lastModified) {
		SQLiteDatabase db=getReadableDatabase();
		
		String query="SELECT * FROM "+TABLE_NAME+" WHERE "+COLUMN_PATH+" = ?";
		Cursor cursor=db.rawQuery(query, new String[] { path });
		
		if (!cursor.moveToFirst()) return null;
		
		while (!cursor.isAfterLast()) {
			long lastModifiedEntry=cursor.getLong(1);
			if (lastModifiedEntry<lastModified) {
				cursor.moveToNext();
				continue;
			}
			
			// It's a valid candidate;
			MP4Metadata metadata=new MP4Metadata();
			metadata.title=cursor.getString(2);
			metadata.titleSort=cursor.getString(3);
			metadata.year=cursor.getInt(4);
			metadata.description=cursor.getString(5);
			metadata.genre=cursor.getString(6);
			metadata.cast=cursor.getString(7);
			metadata.director=cursor.getString(8);
			metadata.coverArt=cursor.getBlob(9);
			
			return metadata;
		}
		
		return null;
	}
}
